package com.huobao.shangjia_version.activitys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.adapters.Adapter_Dingdan_Detail;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.DingdanDetail;
import com.huobao.yunying.entity.DingdanDetail_good_list;
import com.huobao.yunying.entity.DingdanDetail_orderextm;
import com.huobao.yunying.entity.Gouwuche_specification;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.views.NoScrollListView;
import com.huobao.zhangying.R;

import android.widget.Button;
import android.widget.TextView;

import org.xutils.view.annotation.ViewInject;

public class Act_Store_dingdan_detail extends HuobaoBaseActivity {
	@ViewInject(R.id.tv_bianhao)
	private TextView tv_bianhao;//编号
	@ViewInject(R.id.tv_dingdan_zhuangtai)
	private TextView tv_dingdan_zhuangtai;//订单状态
	@ViewInject(R.id.tv_dazhe)
	private TextView tv_dazhe;//优惠打折
	@ViewInject(R.id.tv_zongjia)
	private TextView tv_zongjia;//总价
	@ViewInject(R.id.tv_time)
	private TextView tv_time;//下单日期
	@ViewInject(R.id.tv_hongbao)
	private TextView tv_hongbao;//红包
	@ViewInject(R.id.tv_yunfei)
	private TextView tv_yunfei;//运费
	@ViewInject(R.id.tv_price)
	private TextView tv_price;//应付金额
	@ViewInject(R.id.tv_zhifufangshi)
	private TextView tv_zhifufangshi;//支付方式
	@ViewInject(R.id.tv_peisong)
	private TextView tv_peisong;//配送方式


	@ViewInject(R.id.btn_fukuan)
	private Button btn_fukuan;
	@ViewInject(R.id.listview)
	private NoScrollListView listview;
	@ViewInject(R.id.tv_name)
	private TextView tv_name;
	@ViewInject(R.id.tv_dianhua)
	private TextView tv_dianhua;
	@ViewInject(R.id.tv_address)
	private TextView tv_address;



	private Adapter_Dingdan_Detail adapter;
	
	private String order_id;




	@Override
	protected int getContentView() {
		return R.layout.layout_store_dingdan_detail;
	}

	@Override
	protected String getTitleText() {
		return getString(R.string.title_5);
	}

	@Override
	protected void init() {
		order_id = getIntent().getStringExtra("order_id");
		adapter = new Adapter_Dingdan_Detail(context);
		listview.setAdapter(adapter);
		getData();
	}
	private void getData() {
		Map<String, String> map = HttpUtil.getParams("app", "buyerorder", "act", "view", "order_id", order_id);
		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				JSONObject object = jsonObject.getJSONObject("retval");
				DingdanDetail detail = JSON.parseObject(object.toString(), DingdanDetail.class);
				JSONArray array = object.getJSONArray("good_list");
				List<DingdanDetail_good_list> list = new ArrayList<>();
				for(int i=0;i<array.size();i++){
					JSONObject ob = array.getJSONObject(i);
					DingdanDetail_good_list dg = JSON.parseObject(ob.toString(), DingdanDetail_good_list.class);
					List<Gouwuche_specification> specification = JSON.parseArray(ob.getString("order_spec"),Gouwuche_specification.class);
					dg.specification = specification;
					list.add(dg);
				}

				DingdanDetail_orderextm detail_orderextm = JSON.parseObject(object.getString("orderextm"),
						DingdanDetail_orderextm.class);
				detail.good_list = list;
				detail.orderextm = detail_orderextm;
				adapter.addAll(list);
				show(detail);
			}

			@Override
			public void onHttpError(String error) {

			}
		});
	}
	private DingdanDetail detail;
	protected void show(DingdanDetail detail) {
		this.detail = detail;
		tv_bianhao.setText(detail.order_sn);
		tv_dingdan_zhuangtai.setText(detail.status_text);
		tv_time.setText(detail.add_time);
		tv_price.setText("￥" + detail.order_amount );
		DingdanDetail_orderextm orderextm = detail.orderextm;
		tv_name.setText(orderextm.consignee);
		tv_dianhua.setText(orderextm.phone_mob);
		tv_address.setText(orderextm.region_name + " " + orderextm.address);
		tv_dazhe.setText("￥" + detail.discount);
		tv_yunfei.setText("￥" + orderextm.shipping_fee);
		tv_peisong.setText(orderextm.shipping_name);
		tv_zongjia.setText(detail.alltotal);
		tv_hongbao.setText(detail.bonus);
		tv_zhifufangshi.setText(detail.payment_name);
	}
}
